<script setup>
import { ref, reactive, onMounted, watch } from "vue";
import { onReachBottom } from "@dcloudio/uni-app";
import { request } from "../../../common/http";
import { ApiUrl } from "../../../common/ApiUrl";
import { jumpPage, Toast } from "@/utils";
import NavBar from "../../components/NavBar.vue";

let machine = ref({
	/* 机器编号搜索关键字 */
	keyword: "",
	/* 当前页码 */
	pageNum: 1,
	/* 每页显示的条数 */
	pageSize: 10,
	/* 总共有多少条数据 */
	pageCount: 1,
})
/* 警告库存机器列表 */
let machineList = ref([]);
const state = reactive({
	navHeight: 0,
})

onMounted(() => {
	/* 获取警告库存机器列表 */
	getMachineList();
})

/* 机器编号搜索框确定按钮事件 */
const handleSearch = () => {
	if (!machine.value.keyword) return Toast("请输入机器编号")
	machineList.value = [];
	/* 获取警告库存机器列表 */
	getMachineList();
}

/* 获取警告库存机器列表 */
const getMachineList = () => {
	request({
		url: `${ApiUrl.WarnStockUrl}`,
		method: 'post',
		data: {
			...machine.value
		}
	}).then(res => {
		machineList.value = machineList.value.concat(res.data.list);
		machine.value.pageCount = res.data.pages;
	})
}

/* 补货按钮点击事件-跳转设备管理/货道管理页面 */
const handleReplenishment = (item) => {
	uni.navigateTo({
		url: `/pages/index/Restock/index?machineId=${item.id}`
	})
}

/* 监听机器编号搜索关键字  */
watch(() => machine.value.keyword, (val) => {
	if (!val) {
		machine.value.pageNum = 1;
		machineList.value = [];
		/* 获取警告库存机器列表 */
		getMachineList();
	}
})

/* 上拉加载 */
onReachBottom(() => {
	machine.value.pageNum++;
	if (machine.value.pageNum < machine.value.pageCount + 1 && (machine.value.pageCount != 1)) {
		getMachineList();
	} else {
		// 没有数据了
	}
})

</script>

<template>
  <view class="warn_stock">
		<NavBar v-model:navHeight="state.navHeight" :hideBack="false">
		  <template v-slot:center>
				<view class="input">
					<input v-model.trim="machine.keyword" placeholder="请输入机器编号" @confirm="handleSearch" />
				</view>
		  </template>
		</NavBar>
		
		<scroll-view scroll-y="true">
			<view class="machine_list">
				<block v-if="machineList.length > 0">
					<view class="machine_item" v-for="item in machineList" :key="item.id">
						<view class="machine_no">机器编号：{{ item.machineNo }}</view>
						<view class="machine_info">
							<view>总货道数：<text>{{ item.goodsway }}个</text></view>
							<view>在线状态：<text>{{ item.online ? "在线" : "离线" }}</text></view>
							<view class="warn_ways">预警货道数：{{ item.warnWays }}</view>
							<view class="warn_status">运营状态：{{ item.status == 1 ? "运营中" : item.status == 2 ? "关闭运营" : "虚位" }}</view>
						</view>
						<view class="btn_replenishment">
							<text @click="handleReplenishment(item)">补货</text>
						</view>
					</view>
				</block>
				<view v-else class="no_data">暂无数据</view>
			</view>
		</scroll-view>
	</view>
</template>

<style lang="scss">
.warn_stock {
	box-sizing: border-box;
	.m-nav {
		.nav {
			.center {
				left: unset !important;
				transform: translate(10%, -50%) !important;
			}
		}
	}
	.input {
		width: 436rpx;
	}
	.machine_list {
		padding-top: calc(v-bind('state.navHeight') * 1px + 24rpx);
		.machine_item {
			width: 686rpx;
			background: #FFFFFF;
			border-radius: 8rpx;
			margin: 0 auto 24rpx;
			.machine_no {
				position: relative;
				height: 88rpx;
				box-shadow: inset 0rpx -1rpx 0rpx 0rpx #EBEEF1;
				display: flex;
				align-items: center;
				font-weight: 500;
				font-size: 32rpx;
				color: #303030;
				box-sizing: border-box;
				padding-left: 34rpx;
				&::before {
					content: "";
					position: absolute;
					top: 50%;
					left: 0;
					transform: translateY(-50%);
					width: 6rpx;
					height: 40rpx;
					background: #FF4638;
					border-radius: 3rpx;
				}
			}
			.machine_info {
				width: 100%;
				box-sizing: border-box;
				padding: 32rpx 32rpx 44rpx;
				display: flex;
				flex-wrap: wrap;
				view {
					width: 50%;
					font-size: 24rpx;
					line-height: 36rpx;
					color: #969699;
					&:nth-of-type(-n+2) {
						margin-bottom: 20rpx;
					}
					text {
						color: #303030;
					}
					&.warn_ways {
						color: #FF9832;
					}
					&.warn_status {
						color: #25DFA5;
					}
				}
			}
			.btn_replenishment {
				width: 100%;
				box-shadow: inset 0rpx 1rpx 0rpx 0rpx #EBEEF1;
				box-sizing: border-box;
				padding: 12rpx 30rpx 12rpx 0;
				display: flex;
				justify-content: flex-end;
				text {
					width: 152rpx;
					height: 64rpx;
					background: linear-gradient( 129deg, #FF776A 0%, #FF4638 100%);
					border-radius: 32rpx;
					font-size: 28rpx;
					color: #FFFFFF;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
		.no_data {
			font-size: 24rpx;
			color: #969699;
			text-align: center;
		}
	}
}
</style>